package mtt.view.calendar;

import java.io.IOException;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

import mtt.control.DisplayController;
import mtt.model.calendar.Event;
import mtt.model.record.MttCalendarRecordStore;
import mtt.view.IMttDisplayAble;


public class EventView implements IMttDisplayAble {
	
	private static final Form form 	= new Form("Termindetails");
	
	private static TextField 	textFieldName;
	private static TextField 	textFieldOrt;
	private static ChoiceGroup 	choiceGroupTyp;
	private static ChoiceGroup 	choiceGroupTimeOfDate;
	private static ChoiceGroup 	choiceGroupIntervall;
	private static TextField 	textFieldTutor;

	private static final Command commandSave = new Command("Speichern", Command.OK, 0);
	private static final Command commandBack = new Command("zurück", Command.BACK, 0);
	
	/**
	 * Definiert den Index für den Typ "Vorlesung Obligatorisch"
	 */
	public static final int indexTypVO = 0;
	/**
	 * Definiert den Index für den Typ "Vorlesung Wahlpflicht"
	 */
	public static final int indexTypVW = 1;
	/**
	 * Definiert den Index für den Typ "Praktikum Obligatorisch"
	 */
	public static final int indexTypPO = 2;
	/**
	 * Definiert den Index für den Typ "Praktikum Wahlpflicht"
	 */
	public static final int indexTypPW = 3;
	/**
	 * Definiert den Index für den Typ "Übung Obligatrisch"
	 */
	public static final int indexTypUEO = 4;
	/**
	 * Definiert den Index für den Typ "Lerngruppen Event"
	 */
	public static final int indexTypLT = 5;
	
	/**
	 * Definiert den Index für den Intervall "wöchentlich"
	 */
	public static final int indexIntervallWeekly = 0;
	
	/**
	 * Definiert den Index für den Intervall "alle 2 Wochen"
	 */
	public static final int indexIntervall2Weeks = 1;
	
	/**
	 * Definiert den Index für den Intervall "alle 3 Wochen"
	 */
	public static final int indexIntervall3Weeks = 2;
	
	/**
	 * enthält den aktuell angezeigt Event
	 */
	private static Event event;
	
	private static final EventView eventView = new EventView();
	
	/**
	 * Factory-Methode liefert immer die gleiche Event-View instanz
	 * @param event
	 * @return
	 */
	public static final EventView getInstance(Event event) {
		initEventView(event);
		return eventView;
	}
	
	private EventView() {
		
	}
	
	private static void initEventView(Event event) {
		
		EventView.event = event;
		
		/*
		 * alle Forumlar leeren
		 */
		form.deleteAll();
		
		/*
		 * Textfelder füllen
		 */
		textFieldName	= new TextField("Name: ", 	event.getStringName(), 25, TextField.ANY);
		textFieldOrt	= new TextField("Ort: ", 	event.getStringOrt(), 25, TextField.ANY);
		textFieldTutor 	= new TextField("Dozent: ", event.getStringTutor(), 25, TextField.ANY);
		
		/*
		 * ChoiceGroup Typ erstellen
		 */
		choiceGroupTyp	= new ChoiceGroup("Typ: ", ChoiceGroup.POPUP);
		choiceGroupTyp.insert(indexTypVO, 	getStringOfTyp(indexTypVO), null);
		choiceGroupTyp.insert(indexTypVW, 	getStringOfTyp(indexTypVW), null);
		choiceGroupTyp.insert(indexTypPO, 	getStringOfTyp(indexTypPO), null);
		choiceGroupTyp.insert(indexTypPW, 	getStringOfTyp(indexTypPW), null);
		choiceGroupTyp.insert(indexTypUEO, 	getStringOfTyp(indexTypUEO), null);
		choiceGroupTyp.insert(indexTypLT, 	getStringOfTyp(indexTypLT), null);
		
		if (event.getIntTyp() != null) {
			/*
			 * voreingestellten Index auswählen
			 */
			choiceGroupTyp.setSelectedIndex(event.getIntTyp().intValue(), true);
		}
		
		
		/*
		 * ChoiceGroup TimeOfDate erstellen
		 */
		choiceGroupTimeOfDate = new ChoiceGroup("Uhrzeit: ", ChoiceGroup.POPUP);
		choiceGroupTimeOfDate.insert(Event.index_TIME_07To09, 	Event.getStringOfTimeId(Event.index_TIME_07To09), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_09To11, 	Event.getStringOfTimeId(Event.index_TIME_09To11), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_11To13, 	Event.getStringOfTimeId(Event.index_TIME_11To13), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_13To15, 	Event.getStringOfTimeId(Event.index_TIME_13To15), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_15To17, 	Event.getStringOfTimeId(Event.index_TIME_15To17), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_17To19, 	Event.getStringOfTimeId(Event.index_TIME_17To19), null);
		choiceGroupTimeOfDate.insert(Event.index_TIME_19To21, 	Event.getStringOfTimeId(Event.index_TIME_19To21), null);
		
		if (event.getTimeIndexOfEvent() != null) {
			/*
			 * voreingestellten Index auswählen
			 */
			choiceGroupTimeOfDate.setSelectedIndex(event.getTimeIndexOfEvent().intValue(), true);
		}
		
		
		/*
		 * ChoiceGroup Intervall
		 */
		choiceGroupIntervall = new ChoiceGroup("Intervall: ", ChoiceGroup.POPUP);
		choiceGroupIntervall.insert(indexIntervallWeekly, getStringOfIntervallId(indexIntervallWeekly), null);
		choiceGroupIntervall.insert(indexIntervall2Weeks, getStringOfIntervallId(indexIntervall2Weeks), null);
		choiceGroupIntervall.insert(indexIntervall3Weeks, getStringOfIntervallId(indexIntervall3Weeks), null);
		
		if (event.getIntIntervall() != null) {
			/*
			 * voreingestellten Index auswählen
			 */
			choiceGroupIntervall.setSelectedIndex(event.getIntIntervall().intValue(), true);
		}
		
		
		form.setCommandListener(DisplayController.getInstance());
		
		/*
		 * Element zur Form hinzufügen
		 */
		form.insert(0, textFieldName);
		form.insert(1, choiceGroupTyp);
		form.insert(2, textFieldOrt);
		form.insert(3, choiceGroupTimeOfDate);
		form.insert(4, choiceGroupIntervall);
		form.insert(5, textFieldTutor);
		
		/*
		 * Buttons hinzufügen
		 */
		form.addCommand(commandBack);
		form.addCommand(commandSave);
	}
	
	public void commandAction(Command command) {
		if (command == commandSave) {
			try {
				storeEvent();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (command == commandBack) {
		}

	}

	private void storeEvent() throws IOException {
		
		/*
		 * Parameter aktualisieren
		 */
		refreshEventItems();
		
		MttCalendarRecordStore mttCalendarRecordStore;
		try {
			mttCalendarRecordStore = MttCalendarRecordStore.getInstance();
			
			if (event.getIntId() == null) {
				/*
				 * Event hinzufügen
				 */
				mttCalendarRecordStore.addEvent(event);
			} else {
				/*
				 * Event aktualisieren
				 */
				mttCalendarRecordStore.updateEvent(event);
			}
			
		} catch (RecordStoreFullException e1) {
			DisplayController displayController = DisplayController.getInstance();
			displayController.showAlertError("Es steht kein Speicherplatz mehr zur Verfügung", null);
		} catch (RecordStoreNotFoundException e1) {
			DisplayController displayController = DisplayController.getInstance();
			displayController.showAlertError("RecordStoreNotFoundException: " + e1.getMessage(), null);
		} catch (RecordStoreException e1) {
			DisplayController displayController = DisplayController.getInstance();
			displayController.showAlertError("RecordStoreException: " + e1.getMessage(), null);
		}
		
	}

	/**
	 * aktualisiert die Parameter des Events
	 */
	private void refreshEventItems() {
		/*
		 * Termindaten aktualisieren
		 */
		event.setStringName			(textFieldName.getString());
		event.setTimeIndexOfEvent	(new Integer(choiceGroupTimeOfDate.getSelectedIndex()));
		event.setIntIntervall		(new Integer(choiceGroupIntervall.getSelectedIndex()));
		event.setStringTutor		(textFieldTutor.getString());
		event.setStringOrt			(textFieldOrt.getString());
		event.setIntTyp				(new Integer(choiceGroupTyp.getSelectedIndex()));
	}

	public Displayable getDisplayable() {
		return form;
	}
	
	/**
	 * liefert den vollen Namen zur dazugehörigen Event.indexTyp* -Id
	 * @param Event.indexTyp* Id
	 * @return String der Id oder null wenn es die Id nicht gibt
	 */
	public static String getStringOfTyp(int id) {
		switch (id) {
		case indexTypVO:
			return "VO - Vorlesung Obligatorisch";
		case indexTypVW:
			return "VW - Vorlesung Wahlpflicht";
		case indexTypPO:
			return "PO - Praktikum Obligatorisch";
		case indexTypPW:
			return "PW - Praktikum - Wahlpflicht";
		case indexTypUEO:
			return "ÜO - Übung Obligatorisch";
		case indexTypLT:
			return "LT - Lerngruppen-Event";
		default:
			return null;
		}
	}
	
	/**
	 * liefert den String zur Event.indexIntervall* -Id
	 * @param Event.indexIntervall*
	 * @return Stringvalue of Event.indexIntervall* -Id 
	 */
	public static String getStringOfIntervallId(int id) {
		switch (id) {
		case 0: return "wöchentlich";
		case 1: return "in geraden Wochen";
		case 2: return "in ungeraden Wochen";
		default: return null;
		}
	}
}
